const bcrypt = require('bcryptjs')
// 对象数据转换成sql语句更新数据
exports.convertSQLUpdate = (obj, connector=',') => {
  const result = Object.entries(obj)
  .map(([key, value]) => `${key} = ${typeof value === 'string' ? `"${value}"` : value}`)
  .join(connector);
  return result
}
// 对象数据转换成sql语句插入数据
exports.convertSQLInsert = (arr, connector=', ') => {
  const keys = Object.keys(arr[0])
  const result = `(${keys.join(connector)}) VALUES ${arr.map(obj => `(${keys.map(key => `"${obj[key]}"`).join(connector)})`).join(',')}`;
  return result
}
// 返回前端数据结构
exports.responseResultData = (res, code, isSuccess, errorMsg, data, token='') => {
  console.log('##responseResultData####', code, isSuccess, errorMsg, data, token)
  res.send( {
    code,
    isSuccess,
    errorMsg,
    data,
    token
  })
}
// 随机6位数字
exports. generateRandomNumber = () => {
  let min = 100000; // 最小值
  let max = 999999; // 最大值
  let randomNumber = Math.floor(Math.random() * (max - min + 1)) + min
  return randomNumber
}
// bcryptjs加密
exports.encryption = (passWord) => {
  const hashedPassword = bcrypt.hashSync(passWord, 10)
  return hashedPassword
}
// bcryptjs验证
exports.compareSync = (passWord, hash) => {
  return bcrypt.compareSync(passWord, hash)
}
